package com.wx.service.impl;

import com.wx.dao.UserDao;
import com.wx.model.UserModel;
import com.wx.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    UserDao userDao;


    @Override
    public int delete(Integer id) {
        int result=userDao.delete(id);
        return result;
    }


    @Override
    public UserModel finById(int id) {
        UserModel userModel=userDao.finById(id);
        return userModel;
    }


    @Override
    public int update(UserModel userModel) {
        int result=userDao.update(userModel);
        return result;
    }

//   条件查询
    @Override
    public List<UserModel> conditionFind(UserModel userModel) {
        List<UserModel> user=userDao.conditionFind(userModel);
        return user;
    }

    @Override
    public int getCount(UserModel userModel) {
        int number=userDao.getCount(userModel);
        return number;
    }


    @Override
    public List<UserModel> getAllUser() {
        return userDao.getAllUser();
    }


    @Override
    public int login(UserModel userModel) {
        if (!userModel.getCode().equals(userModel.getSessionCode())){
            return -1;
        }else {
            int result=userDao.findByUsernameAndPassword(userModel);
            return result;
        }
    }


    @Override
    public int addUser(UserModel userModel) {
//        判断添加用户前判断是否数据库存在该用户
        boolean isHave=isHaveFindByUsername(userModel.getUsername());
        if (isHave){
            return 4;
        }else {
            int count=userDao.addUser(userModel);
            return count;
        }
    }

    public boolean isHaveFindByUsername(String username){
        //    根据用户名去查数据库是否存在，如果存在返回true 否则返回false
        UserModel userModel=userDao.FindByUsername(username);
        boolean isHave=(userModel==null);
        return !isHave;
    }

}
